package cn.fornever.tools.db;

import cn.fornever.tools.db.assist.entity.*;
import org.junit.Assert;
import org.junit.Test;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.List;

/**
 * 数据库工具类--测试
 *
 * @author ZhangAY
 */
public class DBUtilTestMysql extends DBUtilTestBase {

    private static String driverClass_MYSQL = "com.mysql.jdbc.Driver";
    private static String connectionURL_MYSQL = "jdbc:mysql://127.0.0.1:3306/jxcmsv8?characterEncoding=UTF-8&useInformationSchema=true";
    private static String userName_MYSQL = "root";
    private static String password_MYSQL = "";

    static JDBCConnectionConfig config_MYSQL = new JDBCConnectionConfig(driverClass_MYSQL, connectionURL_MYSQL, userName_MYSQL, password_MYSQL);

    private String catalog = "jxcmsv8";
    private String schema = "";
    private String tableNamePattern = "%";

    @Test
    public void getDatabaseMetaDataTest() {
        Connection connection = DBUtil.getConnection(config_MYSQL);
        DatabaseMetaData metaData = getDatabaseMetaData(connection);
        Assert.assertTrue(metaData != null);
        DBUtil.closeConnection(connection);
    }

    //http://www.cnblogs.com/lbangel/p/3487796.html
    @Test
    public void getTableInfoTest() {
        Connection connection = DBUtil.getConnection(config_MYSQL);
        List<TableInfo> list = getTableInfo(connection, catalog, schema, tableNamePattern, null);
        DBUtil.closeConnection(connection);
        Assert.assertTrue(list != null);
    }

    @Test
    public void getColumnInfoTest() {
        Connection connection = DBUtil.getConnection(config_MYSQL);
        List<TableInfo> list = getTableInfo(connection,catalog,schema,tableNamePattern,null);
        DBUtil.closeConnection(connection);
        Assert.assertTrue(list != null);
    }

    @Test
    public void getPrimaryKeyInfoTest() {
        Connection connection = DBUtil.getConnection(config_MYSQL);
        List<PrimaryKeyInfo> list = null;
        try {
            list = DBUtil.getPrimaryKeyInfo(connection, connection.getCatalog(), null, "%");
            if (list != null) {
                for (PrimaryKeyInfo info : list) {
                    System.out.println(info.toString());
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DBUtil.closeConnection(connection);
        Assert.assertTrue(list != null);
    }

    @Test
    public void getForeignKeyInfoTest() {
        Connection connection = DBUtil.getConnection(config_MYSQL);
        List<ForeignKeyInfo> list = null;
        try {
            list = DBUtil.getForeignKeyInfo(connection, "jxcmsv8", "", "jc_comment");
            if (list != null) {
                for (ForeignKeyInfo info : list) {
                    System.out.println(info.toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        DBUtil.closeConnection(connection);
        Assert.assertTrue(list != null);
    }

//    public static void main(String[] args) throws IOException, SQLException {
//
//        FileTemplateResolver templateResolver = new FileTemplateResolver();
//        templateResolver.setTemplateMode(TemplateMode.HTML);
//        templateResolver.setPrefix("D:\\资料\\10-网页\\AmazeUI-2.7.2\\");
//        templateResolver.setSuffix(".html");
//        templateResolver.setCharacterEncoding("UTF-8");
//        TemplateEngine templateEngine = new TemplateEngine();
//        templateEngine.setTemplateResolver(templateResolver);
//
//        Connection connection = DBUtil.getConnection(config_MYSQL);
//
//        Map<String,Object> variables = new HashMap<String,Object>();
//        variables.put("today2","121");
//        List<TableInfo> list = DBUtil.getTableInfo(connection, connection.getCatalog(), null, null, null);
//        variables.put("tableList",list);
//        Context context = new Context(Locale.CHINESE,variables);
//        File file = new File("D:\\资料\\10-网页\\AmazeUI-2.7.2\\index1.html");
//        if (!file.exists()){
//            file.createNewFile();
//        }
//        FileWriter fileWriter = new FileWriter(file);
//        templateEngine.process("_index", context, fileWriter);
//        fileWriter.flush();
//        fileWriter.close();
//        System.out.println(templateEngine.isInitialized());
//    }

}